package com.gzx.risk.core.apis.context;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class DaysCalculator {

	public static int calculateDays(LocalDate date) {
		LocalDate currentDate = LocalDate.now();
		if (date.isAfter(currentDate)) {
			return 0;
		}

		Period period = Period.between(date, currentDate);
		return period.getDays();
	}

	public static int calculateDays(String dateStr) throws DateTimeParseException {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		LocalDate date = LocalDate.parse(dateStr, formatter);
		return calculateDays(date);
	}

	public static void main(String[] args) {
		// 测试示例
		try {
			// 测试2：使用字符串
			String dateStr = "20250209"; // 闰年2月29日
			System.out.println("days：" + calculateDays(dateStr));

		} catch (DateTimeParseException e) {
			System.out.println("日期格式错误：" + e.getMessage());
		} catch (IllegalArgumentException e) {
			System.out.println("参数错误：" + e.getMessage());
		}
	}
}
